package net.uhuijia.user.userCart.controller;

import net.uhuijia.enums.MoreInfo;
import net.uhuijia.user.userCart.persistence.model.UserCart;
import net.uhuijia.user.userCart.persistence.model.UserCartExample;
import net.uhuijia.user.userCartItem.persistence.model.UserCartItem;
import net.uhuijia.user.userCartItem.service.UserCartItemService;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.utils.IticConst;
import net.uhuijia.vo.ResultMapVo;
import net.uhuijia.vo.ReturnListVo;
import net.uhuijia.vo.cart.UserCartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import net.uhuijia.appbase.controller.BaseController;
import net.uhuijia.user.userCart.service.UserCartService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping(value="/userCart")
public class UserCartController
     extends BaseController<UserCartService,UserCart,UserCartExample> {
    @Autowired
    private UserCartService userCartService;
    @Autowired
    private UserCartItemService userCartItemService;
    /**
     * @Author xzj
     * @Description //添加购物车
     * @Date  2018/10/7
     * @Param
     * @return
     **/
    @RequestMapping(value = "/addCart" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultMapVo addCart(String spuId,Integer count,String propertyId ) {
        if(!StringUtils.isNoneBlank(spuId,propertyId) || count ==null)
            throw  new IllegalArgumentException();
        UserMall userMall = this.getUserMallByTokenAndDevice();
        UserCartItem userCartItem = this.userCartService.addCart(userMall,spuId,count,propertyId);
        return ResultMapVo.ok();
    }
    /**
     * @Author xzj
     * @Description // 购物车列表
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    @RequestMapping(value = "/cartList" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultMapVo<ReturnListVo<UserCartVo>> cartList(HttpServletRequest request) {
        //获取用户信息
        UserMall userMall = this.getUserMallByTokenAndDevice();
        UserCartExample example = new UserCartExample();
        IticConst.handlePageQueryDefault(request,example);
        List<UserCartVo> userCartVos = this.userCartService.findCartListByUserIdAndPage(userMall,example);
        int count = this.userCartService.findUserCartCountByUserId(userMall.getId());
        MoreInfo moreInfo = IticConst.isMore(request,count);
        return ResultMapVo.ok(new ReturnListVo<>(userCartVos,count,moreInfo));
    }
    /**
     * @Author xzj
     * @Description //删除购物车商品项
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    @RequestMapping(value = "/delCartItem" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultMapVo delCartItem(String cartItemId){
        if(StringUtils.isEmpty(cartItemId))
            throw new  IllegalArgumentException();
        UserMall userMall = this.getUserMallByTokenAndDevice();
        this.userCartItemService.delCartItem(userMall,cartItemId);
        return ResultMapVo.ok();
    }


}